using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VKSGK
    {
        public DataRow Convert2KSGK(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryKSGK(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KSGMANDANT"] = " ".PadRight(2 ); 
         dwReturn["KSGKTAB"] = " ".PadRight(5 ); 
         dwReturn["KSGZTR"] = " ".PadRight(1 ); 
         dwReturn["KSGVART"] = " ".PadRight(5 ); 
         dwReturn["KSGMERKMA"] = " ".PadRight(8 ); 
         dwReturn["KSGKSATZ01"] = " ".PadRight(0 ); 
         dwReturn["KSGKSATZ02"] = " ".PadRight(0 ); 
         dwReturn["KSGKSATZ03"] = " ".PadRight(0 ); 
         dwReturn["KSGKSATZ04"] = " ".PadRight(0 ); 
         dwReturn["KSGKSATZ05"] = " ".PadRight(0 ); 
         dwReturn["KSGME"] = " ".PadRight(1 ); 
         dwReturn["KSGPROZKZ"] = " ".PadRight(1 ); 
         dwReturn["KSGFILLER"] = " ".PadRight(23 ); 
         dwReturn["KSGAENZL"] = " ".PadRight(0 ); 
         dwReturn["KSGAENB"] = " ".PadRight(8 ); 
         dwReturn["KSGAEND"] = " ".PadRight(0 ); 
         dwReturn["KSGNEUB"] = " ".PadRight(8 ); 
         dwReturn["KSGNEUD"] = " ".PadRight(0 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapKSGK(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveKSGK(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryKSGK(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapKSGK(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertKSGK(dwLocal, ITXDBVERSION);
            }
            UpdateKSGK(dwLocal);
        }
        public void InsertKSGK(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO KSGK(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryKSGK(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KSGK WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM KSGK  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "KSGK";
            Return = Result;
            return Return;
        }
        public void DeleteKSGK(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM KSGK   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateKSGK(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE KSGK SET "
                    +"KSGMANDANT= :KSGMANDANT,"
                    +"KSGKTAB= :KSGKTAB,"
                    +"KSGZTR= :KSGZTR,"
                    +"KSGVART= :KSGVART,"
                    +"KSGMERKMA= :KSGMERKMA,"
                    +"KSGKSATZ01= :KSGKSATZ01,"
                    +"KSGKSATZ02= :KSGKSATZ02,"
                    +"KSGKSATZ03= :KSGKSATZ03,"
                    +"KSGKSATZ04= :KSGKSATZ04,"
                    +"KSGKSATZ05= :KSGKSATZ05,"
                    +"KSGME= :KSGME,"
                    +"KSGPROZKZ= :KSGPROZKZ,"
                    +"KSGFILLER= :KSGFILLER,"
                    +"KSGAENZL= :KSGAENZL,"
                    +"KSGAENB= :KSGAENB,"
                    +"KSGAEND= :KSGAEND,"
                    +"KSGNEUB= :KSGNEUB,"
                    +"KSGNEUD= :KSGNEUD,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KSGMANDANT", OracleType.NVarChar).Value = dwData["KSGMANDANT"].ToString(); 
          or1.Parameters.Add("KSGKTAB", OracleType.NVarChar).Value = dwData["KSGKTAB"].ToString(); 
          or1.Parameters.Add("KSGZTR", OracleType.NVarChar).Value = dwData["KSGZTR"].ToString(); 
          or1.Parameters.Add("KSGVART", OracleType.NVarChar).Value = dwData["KSGVART"].ToString(); 
          or1.Parameters.Add("KSGMERKMA", OracleType.NVarChar).Value = dwData["KSGMERKMA"].ToString(); 
          or1.Parameters.Add("KSGKSATZ01", OracleType.NVarChar).Value = dwData["KSGKSATZ01"].ToString(); 
          or1.Parameters.Add("KSGKSATZ02", OracleType.NVarChar).Value = dwData["KSGKSATZ02"].ToString(); 
          or1.Parameters.Add("KSGKSATZ03", OracleType.NVarChar).Value = dwData["KSGKSATZ03"].ToString(); 
          or1.Parameters.Add("KSGKSATZ04", OracleType.NVarChar).Value = dwData["KSGKSATZ04"].ToString(); 
          or1.Parameters.Add("KSGKSATZ05", OracleType.NVarChar).Value = dwData["KSGKSATZ05"].ToString(); 
          or1.Parameters.Add("KSGME", OracleType.NVarChar).Value = dwData["KSGME"].ToString(); 
          or1.Parameters.Add("KSGPROZKZ", OracleType.NVarChar).Value = dwData["KSGPROZKZ"].ToString(); 
          or1.Parameters.Add("KSGFILLER", OracleType.NVarChar).Value = dwData["KSGFILLER"].ToString(); 
          or1.Parameters.Add("KSGAENZL", OracleType.NVarChar).Value = dwData["KSGAENZL"].ToString(); 
          or1.Parameters.Add("KSGAENB", OracleType.NVarChar).Value = dwData["KSGAENB"].ToString(); 
          or1.Parameters.Add("KSGAEND", OracleType.NVarChar).Value = dwData["KSGAEND"].ToString(); 
          or1.Parameters.Add("KSGNEUB", OracleType.NVarChar).Value = dwData["KSGNEUB"].ToString(); 
          or1.Parameters.Add("KSGNEUD", OracleType.NVarChar).Value = dwData["KSGNEUD"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
